package com.mingqi.enetconnected.primary.mytask.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseActivity;
import com.mingqi.enetconnected.base.BaseFragment;
import com.mingqi.enetconnected.primary.mytask.adapter.TaskTypePagerAdapter;
import com.mingqi.enetconnected.primary.mytask.event.HintNumberEvent;
import com.mingqi.enetconnected.primary.mytask.event.ScrollEvent;
import com.mingqi.enetconnected.primary.mytask.taskDetails.CompleteInfoFragment;
import com.mingqi.enetconnected.primary.mytask.taskDetails.DeviceInfoFragment;
import com.mingqi.enetconnected.primary.mytask.taskDetails.SafetyTroubleFragment;
import com.mingqi.enetconnected.primary.mytask.taskDetails.ServiceContentFragment;
import com.mingqi.enetconnected.primary.mytask.taskDetails.UserInfoFragment;
import com.mingqi.enetconnected.util.resource.ResourceUtils;
import com.mingqi.enetconnected.view.CustomViewPager;
import com.mingqi.enetconnected.view.tablayout.MsgView;
import com.mingqi.enetconnected.view.tablayout.SlidingTabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TaskDetailsActivity extends BaseActivity {

    @BindView(R.id.stl_task_type)
    SlidingTabLayout mStlTaskType;
    @BindView(R.id.vp_task_details)
    CustomViewPager mVpTaskDetails;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_task_details;
    }

    @Override
    protected void initView() {

        setTitleName(ResourceUtils.getString(R.string.task_details));
    }

    @Override
    protected void initData() {

        List<BaseFragment> fragments = new ArrayList<>();
        UserInfoFragment userInfoFragment = new UserInfoFragment();
        DeviceInfoFragment deviceInfoFragment = new DeviceInfoFragment();
        SafetyTroubleFragment safetyTroubleFragment = new SafetyTroubleFragment();
        ServiceContentFragment serviceContentFragment = new ServiceContentFragment();
        CompleteInfoFragment completeInfoFragment = new CompleteInfoFragment();

        fragments.add(userInfoFragment);
        fragments.add(deviceInfoFragment);
        fragments.add(safetyTroubleFragment);
        fragments.add(serviceContentFragment);
        fragments.add(completeInfoFragment);

        List<TextView> textViews = new ArrayList<>();
        TextView tvUserInfo = new TextView(mContext);
        tvUserInfo.setText(ResourceUtils.getString(R.string.user_info));
        tvUserInfo.setGravity(View.TEXT_ALIGNMENT_CENTER);

        TextView tvDeviceInfo = new TextView(mContext);
        tvDeviceInfo.setText(ResourceUtils.getString(R.string.device_info));
        tvDeviceInfo.setGravity(View.TEXT_ALIGNMENT_CENTER);

        TextView tvSafetyTrouble = new TextView(mContext);
        tvSafetyTrouble.setText(ResourceUtils.getString(R.string.safety_trouble));
        tvSafetyTrouble.setGravity(View.TEXT_ALIGNMENT_CENTER);

        TextView tvServiceContent = new TextView(mContext);
        tvServiceContent.setText(ResourceUtils.getString(R.string.service_content));
        tvServiceContent.setGravity(View.TEXT_ALIGNMENT_CENTER);

        TextView tvCompleteInfo = new TextView(mContext);
        tvCompleteInfo.setText(ResourceUtils.getString(R.string.complete_info));
        tvCompleteInfo.setGravity(View.TEXT_ALIGNMENT_CENTER);

        textViews.add(tvUserInfo);
        textViews.add(tvDeviceInfo);
        textViews.add(tvSafetyTrouble);
        textViews.add(tvServiceContent);
        textViews.add(tvCompleteInfo);

        mVpTaskDetails.setAdapter(new TaskTypePagerAdapter(getSupportFragmentManager(),textViews,fragments));
        mVpTaskDetails.setOffscreenPageLimit(5);
        mStlTaskType.setViewPager(mVpTaskDetails);

        mStlTaskType.showMsg(2, 5);
        mStlTaskType.setMsgMargin(2, 0, 10);

        MsgView rtv_2_3 = mStlTaskType.getMsgView(3);
        if (rtv_2_3 != null) {
            rtv_2_3.setBackgroundColor(ResourceUtils.getColor(R.color.tv_bg_alarm));
            rtv_2_3.setTextColor(ResourceUtils.getColor(R.color.white));
        }

        mVpTaskDetails.setScanScroll(false);
        mStlTaskType.setCanClick(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setCanScroll(ScrollEvent event){

        if(event.isCanScroll()) {

            //点击
            mStlTaskType.setCanClick(true);
            //滑动
            mVpTaskDetails.setScanScroll(true);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setHintNumber(HintNumberEvent event){

        //消息数
        mStlTaskType.hideMsg(2);
    }
}
